using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Ent.Voice.Nomination;
using Ent.Voice.Show;
using Ent.Web.Helper;
using Ent.Web.WebControl;
using System.Collections.Generic;
using System.Drawing;
public enum EntriesOf { User, Event }
public enum Align { Horizontal, Vertical } 
public partial class Controls_RandomEntries : System.Web.UI.UserControl
{
    private string _headerText = "";
    public string HeaderText
    {
        get { return _headerText; }
        set { _headerText = value; }
    }
    private EntriesOf _entriesOf = EntriesOf.Event;
    public EntriesOf EntriesOf
    {
        get { return _entriesOf; }
        set { _entriesOf = value; }
    }
    private string _Id = "1";
    public string Id
    {
        get { return _Id; }
        set { _Id = value; }
    }
    private int _pageIndex = 1;
    public int PageIndex
    {
        get { return _pageIndex; }
        set { _pageIndex = value; }
    }
    private int _count = 10;
    public int Count
    {
        get { return _count; }
        set { _count = value; }
    }
    private OrderBy _orderby = OrderBy.None;
    public OrderBy OrderBy
    {
        get { return _orderby; }
        set { _orderby = value; }
    }
    private Align _align = Align.Vertical;
    public Align Align
    {
        get { return _align; }
        set { _align = value; }
    }
    private Unit _width = Unit.Percentage(100);
    public Unit Width
    {
        get { return _width; }
        set { _width = value; }
    }
    private Unit _height = Unit.Percentage(100);
    public Unit Height
    {
        get { return _height; }
        set { _height = value; }
    }
    private Color _headerColor = System.Drawing.Color.FromArgb(24, 24, 160);
    public Color HeaderColor
    {
        get { return _headerColor; }
        set { _headerColor = value; }
    }
    private DashEntry _entryTemplate = new DashEntry();
    [PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(TemplateControl))]
    public DashEntry EntryTemplate
    {
        get { return _entryTemplate; }
        set { _entryTemplate = value; }
    }
    public Controls_RandomEntries()
    {
        this.PreRender += new EventHandler(Controls_RandomEntries_PreRender);
    }

    void Controls_RandomEntries_PreRender(object sender, EventArgs e)
    {
        SetControl();
    }
    private void SetControl()
    {
        int total = 0;
        int start = ((_pageIndex - 1) * _count) + 1;
        Entry[] entries = null;
        lblHeader.Text = "<div class='randomEntryHeader'>";
        switch (_entriesOf)
        {
            case EntriesOf.User:
                entries = Ent.Voice.User.GetEntriesWithinRange(_Id.ToString(), start, _count, false, StringHelper.ConvertToString(Session["USERID"]), ref total, _orderby);
                //**** Header ****
                lblHeader.Text += _headerText;
                break;
            case EntriesOf.Event:
                entries = Event.GetEntriesWithinRange(Convert.ToInt32(_Id), start, _count, false, StringHelper.ConvertToString(Session["USERID"]), ref total, _orderby);
                //**** Header ****
                lblHeader.Text += ResourceHelper.Get("LBL_OtherNom");
                break;
            default:
                break;
        }
        lblHeader.Text += "</div>";

        if (entries == null)
            return;
        //**** Create Table
        TableCell tc = new TableCell();
        TableRow tr = new TableRow();
        for (int i = 0; i < entries.Length; i++)
        {
            DashEntry de = (DashEntry)CreateDashEntry((Entry)entries.GetValue(i));
            tc = new TableCell();
            tc.Controls.Add(de);
            tc.VerticalAlign = VerticalAlign.Top;
            tc.Height = Unit.Percentage(100);
            tr.Cells.Add(tc);
            if (_align == Align.Vertical || i == entries.Length - 1)
            {
                tblEntries.Rows.Add(tr);
                tr = new TableRow();
            }
        }

        //**** Footer ****
        Dictionary<string, string> kvList = new Dictionary<string, string>();
        kvList.Add("o", Convert.ToString(Entry.GetOrderBy(_orderby)));
        kvList.Add("pi", "");
        bool traverse = false;
        lblFooter.Text = "<table class='general' cellpadding='0' cellspacing='0' style='background-color:" + StringHelper.ConvertColorToHexString(HeaderColor) + "'><tr><td class='generaltd' style='width:50%;padding-left: 5px;'>";
        if (_pageIndex > 1)
        {
            traverse = true;
            kvList["pi"] = Convert.ToString(_pageIndex - 1);
            lblFooter.Text += "<img class='pointer' src='Images/dj/prev.gif' style='width: 20px;' onmouseover=\"this.src='Images/dj/prevh.gif'\"  onmouseout=\"this.src='Images/dj/prev.gif'\" onclick=\"window.location.href='" + Common.ResetUrl(kvList) + "';\" />";
        }
        lblFooter.Text += "</td><td class='generaltd' style='width:50%;padding-right: 5px;' align='right'>";
        if (total > start + _count)
        {
            traverse = true;
            kvList["pi"] = Convert.ToString(_pageIndex + 1);
            lblFooter.Text += "<img class='pointer' src='Images/dj/next.gif' style='width: 20px' onmouseover=\"this.src='Images/dj/nexth.gif'\" onmouseout=\"this.src='Images/dj/next.gif'\" onclick=\"window.location.href='" + Common.ResetUrl(kvList) + "';\" />";
        }
        lblFooter.Text += "</td></tr></table>";
        if (!traverse)
        {
            lblFooter.Visible = false;
            if (_height.Type == UnitType.Pixel)
                _height = Unit.Pixel(Convert.ToInt32(_height.Value + 22));
            divScroll.Style.Add(HtmlTextWriterStyle.Height, StringHelper.ConvertUnitToString(_height));
        }
        else
            divScroll.Style.Add(HtmlTextWriterStyle.Height, StringHelper.ConvertUnitToString(_height));

        if (_align == Align.Horizontal || entries.Length <= 4)
        {
            divScroll.Attributes.Add("class", "border1");
            if (_width.Type == UnitType.Pixel)
                _width = Unit.Pixel(Convert.ToInt32(_width.Value + 14));
        }
        else
        {
            divScroll.Attributes.Add("class", "scrolldiv border1");
            if (_width.Type == UnitType.Pixel)
                _width = Unit.Pixel(Convert.ToInt32(_width.Value));
        }
        tblEntries.Width = _width;
    }
    private iControls_DashEntry CreateDashEntry(Entry entry)
    {
        iControls_DashEntry de = (iControls_DashEntry)LoadControl("DashEntry.ascx");
        int MaxInRow = 1;
        if (_align == Align.Horizontal)
            MaxInRow = _count;
        Common.SetDashEntry(ref de, this.EntryTemplate, MaxInRow);
        de.Entry = entry;
        de.OrderBy = Entry.GetOrderBy(_orderby);
        //de.Height = Unit.Pixel(95);
        return de;
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}
